package com.songlanyun.modules.rabbitmq.interactmsg.impl;

import com.songlanyun.common.enums.OpusConstant;
import com.songlanyun.common.enums.PrivateMessageConstant;
import com.songlanyun.common.enums.SysConstant;
import com.songlanyun.common.utils.push.GeTuiUtils;
import com.songlanyun.common.utils.push.Msg;
import com.songlanyun.modules.account.entity.UserInfoEntity;
import com.songlanyun.modules.account.service.UserInfoService;
import com.songlanyun.modules.message.entity.PrivateMessage;
import com.songlanyun.modules.message.model.InteractionMessage;
import com.songlanyun.modules.message.model.to.MessageTO;
import com.songlanyun.modules.message.model.to.PrivateMessageTO;
import com.songlanyun.modules.message.service.PrivateMessageService;
import com.songlanyun.modules.opus.entity.Opus;
import com.songlanyun.modules.opus.service.OpusService;
import com.songlanyun.modules.rabbitmq.interactmsg.IInteractMsgService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;


/**
 * 文章点赞、分享、评论   --  互动消息
 */
@Service("opusBbsInteractService")
public class OpusBbsInteractServiceImpl implements IInteractMsgService {

    @Resource
    private OpusService opusService;
    @Resource
    private UserInfoService userInfoService;

    @Resource
    private PrivateMessageService privateMessageService;

    @Resource
    private GeTuiUtils geTuiUtils;

    @Override
    public void savePrivateMsg(MessageTO to) {
        Opus opus = opusService.getById(to.getModelId());
        UserInfoEntity user = userInfoService.getById(to.getUserId());
        String remark = String.format("%s了你的%s【%s】", user.getNickname() + to.getBbsType().getMsg(),
                OpusConstant.ModelType.OPUS.getMsg(), opus.getTitle());
        InteractionMessage interactionMessage = new InteractionMessage(SysConstant.InteractionType.OPUS_INTERACTION, to.getModelId(), remark);
        PrivateMessageTO privateMessageTO = new PrivateMessageTO(opus.getPublisherId(), PrivateMessageConstant.MessageType.INTERACTION_MESSAGE, interactionMessage, remark);

        PrivateMessage privateMessage = new PrivateMessage(privateMessageTO);
        privateMessageService.save(privateMessage);

        //单推
        geTuiUtils.pushByUserId(
                Msg.builder()
                        .uid( opus.getPublisherId())
                        .title(PrivateMessageConstant.MessageType.INTERACTION_MESSAGE.getMsg())
                        .content(remark)
                        .msg(privateMessage.pushMessageBody())
                        .build()
        );
    }
}
